<?php

namespace app\api\controller;



use app\api\model\house\House as HouseModel;
use app\common\controller\Api;
use app\api\model\house\HouseEvaluate as HouseEvaluateModel;
use app\api\model\house\HouseEvaluateZan as HouseEvaluateZanModel;
use app\api\model\house\HouseEvaluateComment as HouseEvaluateCommentModel;

class Houseevaluate extends Api
{

    protected $noNeedRight = ['*'];
    protected $noNeedLogin = ['lists'];

    //点评排名
    public function ranking()
    {
        $model = new HouseModel();
        $param = input();
        $list = $model->ranking($param);
        $this->success('',$list);
    }

    //我的点评
    public function myEvaluate()
    {
        $page_size = input('page_size',10);

        $where = ['e.user_id'=>$this->auth->id];

        $list = HouseEvaluateModel::alias('e')
            ->join('user u','e.user_id = u.id')
            ->join('house h','e.house_id = h.house_id')
            ->join('house_order ho','e.order_id = ho.order_id')
            ->where($where)
            ->field('e.*,u.nickname,u.avatar,ho.checkin_date')
            ->order('e.createtime desc')
            ->paginate($page_size, false, ['query' => request()->get()]);

        $this->success('',$list);

    }

    //删除我的点评
    public function delMyEvaluate()
    {
        $evaluate_id = input('evaluate_id',0);

        $info = HouseEvaluateModel::get(['evaluate_id'=>$evaluate_id,'user_id'=>$this->auth->id]);

        if (!$info)$this->error('不是当前用户评价');
        $info->delete();
        $this->success('');
    }

    //我收到的点评
    public function myReceiveEvaluate()
    {
        $page_size = input('page_size',10);

        $where = ['e.owner_id'=>$this->auth->id];

        $list = HouseEvaluateModel::alias('e')
            ->join('user u','e.user_id = u.id')
            ->join('house h','e.house_id = h.house_id')
            ->join('house_order ho','e.order_id = ho.order_id')
            ->where($where)
            ->field('e.*,u.nickname,u.avatar,ho.checkin_date')
            ->order('e.createtime desc')
            ->paginate($page_size, false, ['query' => request()->get()]);

        $this->success('',$list);

    }

    //房屋评价列表
    public function lists()
    {
        $page_size = input('page_size',10);
        $house_id = input('house_id',0);

        $where = ['e.house_id'=>$house_id];

        $list = HouseEvaluateModel::alias('e')
            ->join('user u','e.user_id = u.id')
            ->join('house h','e.house_id = h.house_id')
            ->join('house_order ho','e.order_id = ho.order_id')
            ->where($where)
            ->field('e.*,u.nickname,u.avatar,ho.checkin_date')
            ->order('e.createtime desc')
            ->paginate($page_size, false, ['query' => request()->get()]);
        foreach ($list as $k=>$v){
            if ($this->auth->id){
                $info = HouseEvaluateZanModel::where(['evaluate_id'=>$v['evaluate_id'], 'user_id' => $this->auth->id])->find();
                $list[$k]['is_zan'] = $info?1:0;
            }
            $list[$k]['comment_num'] = HouseEvaluateCommentModel::where(['evaluate_id'=>$v['evaluate_id']])->count();
        }
        $this->success('',$list);

    }

    //提交评价
    public function add()
    {
        try {
            $model = new HouseEvaluateModel();
            $param = input();
            $param['user_id'] = $this->auth->id;
            $res = $model->add($param);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('提交成功',$res);

    }

    //业主回复评价
    public function reply()
    {
        try {
            $model = new HouseEvaluateModel();
            $param = input();
            $param['owner_id'] = $this->auth->id;
            $res = $model->reply($param);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('提交成功',$res);
    }

}